package org.erepublik.company.service;

import java.util.ArrayList;

import org.erepublik.company.Hospital;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONHospitalService extends JSONProductService<Hospital>  {

    public JSONHospitalService() {
        super("hospitals");
    }

    @Override
    protected Hospital[] getEmptyTable() {
        return new Hospital[0];
    }

    @Override
    protected Hospital[] getProductFromJSONArray(JSONArray jsonArray) throws JSONException {
        return getHospitalsFromJSONArray(jsonArray).toArray(new Hospital[0]);
    }

    
    public ArrayList<Hospital> getHospitalsInRegion(String region) {
        
        ArrayList<Hospital> hospitals = new ArrayList<Hospital>();
        
        for (Hospital hospital : allProducts) {
            if (hospital.getRegionName().equals(region))
                hospitals.add(hospital);
        }
        return hospitals;
    }

    
    public static ArrayList<Hospital> getHospitalsFromJSONArray(JSONArray jsonArray) throws JSONException {
        ArrayList<Hospital> data = new ArrayList<Hospital>();
        for (int i = 0; i < jsonArray.length(); i++) {
            data.add( Hospital.fromJSONObject( (JSONObject)jsonArray.get(i) ) );
        }
        return data;
    }  

}
